//named mathx.h to avoid conflicting with standard math.h
#ifndef _MATHX_H_INCLUDED_
#define _MATHX_H_INCLUDED_

#ifdef __cplusplus
extern "C" {
#endif

#ifndef PI
#define PI 3.141592654
#endif

#define LESSEROF(x, y) (x < y ? x : y)
#define GREATEROF(x, y) (x > y ? x : y)
#define DIFF(x, y) ((x > y) ? x - y : y - x)
#define BIT(x) (1 << (x))
#define BITLL(x) (1LL << (long long)x)
#define BITS(val, x, y) (((val) >> (x)) & ((1 << (((y) - (x)) + 1)) - 1)) //extract bits x through y of val
#define HIPART(b) ((b) >> 4) //high nybble of a byte
#define LOWPART(b) ((b) & 0xF) //low nybble

#define BYTESWAP16(x) (short)(((x & 0xFF00) >> 8) | ((x & 0xFF) << 8))
#define BYTESWAP32(x) (int)(((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | ((x & 0x000000FF) << 24))
#define BYTESWAP16U(x) (unsigned short)(((x & 0xFF00) >> 8) | ((x & 0xFF) << 8))
#define BYTESWAP32U(x) (unsigned int)(((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | ((x & 0x000000FF) << 24))
#define BYTES2WORD(b1, b2, b3, b4) ((((b1) & 0xFF) << 24) | (((b2) & 0xFF) << 16) | (((b3) & 0xFF) << 8) | ((b4) & 0xFF))

//Converts degrees to radians
#define RAD(x) ((x) * (PI / 180.0))
#define SINR(x) sin(RAD(x))
#define COSR(x) cos(RAD(x))
#define TANR(x) tan(RAD(x))

#include <stdlib.h>


int RandInt(int Min, int Max);
float RandFloat(float Min, float Max);
void UniqueRand(int* RandPtr, int ArrSize);
int Exp(int Num, int Power);
int Average(int* Arr, int Num);
unsigned int BitToIndex(unsigned int Bit);
unsigned int NextPowerOf2(unsigned int Num);

#ifdef __cplusplus
}
#endif

#endif //_MATHX_H_INCLUDED_
